import { useMessage } from '@/hooks/useMessage';
import { ref } from 'vue';
import { GetReleaseListResultModel } from '@/apis/system/model/informationModel';

import {
  getReleaseInfoDetail,
  topRelease,
  withdrawRelease,
  deleteRelease,
  cancelRelease
} from '@/apis/system/information';
export const useInfo = (refresh: Fn) => {
  const { createConfirm, createMessage } = useMessage();

  const selectId = ref<number | undefined>(undefined);
  const selectType = ref('add');
  const addOrEditTaskVisible = ref(false);
  const dialogVisible = ref(false);
  const topLoading = ref(false);

  const addTask = () => {
    selectType.value = 'add';
    selectId.value = undefined;
    addOrEditTaskVisible.value = true;
  };

  function edit(row: GetReleaseListResultModel) {
    selectType.value = 'edit';
    selectId.value = row.id;
    addOrEditTaskVisible.value = true;
  }
  function handleWithdraw(row: any) {
    createConfirm('确认撤回该信息？', '撤回后需要重新发布！', {
      iconType: 'warning',
      confirmButtonText: '确 认'
    }).then(() => {
      withdrawRelease(row.id).then((res: any) => {
        createMessage.success('撤回成功！');
        refresh();
      });
    });
  }
  const handleDel = (row: any) => {
    createConfirm('提示', '确认删除该信息？', {
      iconType: 'warning',
      confirmButtonText: '删 除'
    }).then(() => {
      deleteRelease(row.id).then(() => {
        createMessage.success('删除成功！');
        refresh();
      });
    });
  };
  const handleTop = (row: GetReleaseListResultModel) => {
    topLoading.value = true;
    topRelease(row.id)
      .then(() => {
        createMessage.success('置顶成功！');
        refresh();
      })
      .finally(() => {
        topLoading.value = false;
      });
  };

  function cancelTop(row: GetReleaseListResultModel) {
    topLoading.value = true;
    cancelRelease(row.id)
      .then(() => {
        createMessage.success('取消置顶成功！');
        refresh();
      })
      .finally(() => {
        topLoading.value = false;
      });
  }

  function handleRelease(row: GetReleaseListResultModel) {
    selectType.value = 'release';
    selectId.value = row.id;
    addOrEditTaskVisible.value = true;
  }

  const showRelease = (row: GetReleaseListResultModel) => {
    const tmpContent = row.content.replace(/[\r\n]/g, '');
    return row.title && row.subtitle && tmpContent;
  };
  const dialogMain = ref<any>({});
  const seeDetail = (row: GetReleaseListResultModel) => {
    if (row.sendType == 'LINK_URL') {
      window.open(row.content);
    } else {
      getReleaseInfoDetail(row.id).then(res => {
        if (row.sendType == 'TEXT_FILE') {
          dialogMain.value = res;
          dialogVisible.value = true;
        } else {
          window.location.href = res.files[0].fileUrl;
        }
      });
    }
  };
  return {
    handleDel,
    addTask,
    edit,
    handleTop,
    handleWithdraw,
    showRelease,
    seeDetail,
    handleRelease,
    cancelTop,
    dialogVisible,
    selectId,
    dialogMain,
    addOrEditTaskVisible
  };
};
